/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 商店模块
	/// module : shop
	/// </summary>
	public interface CNetShop : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction buyResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<BuyResultData>)wrapper;
                SC_buyResult(session, ref _wrapper.message);
                MessageWrapperPool<BuyResultData>.Put(_wrapper);
            };
            MessageFunction refreshResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<RefreshResultData>)wrapper;
                SC_refreshResult(session, ref _wrapper.message);
                MessageWrapperPool<RefreshResultData>.Put(_wrapper);
            };
            MessageFunction updateShop_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<UpdateShopData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.shop_updateShop, "Recv->" + _wrapper.message.ToString());
#endif
                SC_updateShop(session, _wrapper.message.shop);
                MessageWrapperPool<UpdateShopData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.shop_buyResult, (session, package) => {
                	var result = MessageWrapperPool<BuyResultData>.Get();
                	result.Init(session, buyResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.shop_refreshResult, (session, package) => {
                	var result = MessageWrapperPool<RefreshResultData>.Get();
                	result.Init(session, refreshResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.shop_updateShop, (session, package) => {
                	var result = MessageWrapperPool<UpdateShopData>.Get();
                	result.Init(session, updateShop_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
		public void SC_buyResult(ISession session, ref BuyResultData message) 
		{
            _SC_buyResult(session, ref message);
		}
		
		public void _SC_buyResult(ISession session, ref BuyResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.shop_buyResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.shop_buyResult, message.code);
			MsgAsyncAwaiter<BuyResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_refreshResult(ISession session, ref RefreshResultData message) 
		{
            _SC_refreshResult(session, ref message);
		}
		
		public void _SC_refreshResult(ISession session, ref RefreshResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.shop_refreshResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.shop_refreshResult, message.code);
			MsgAsyncAwaiter<RefreshResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 商店更新
        /// </summary>
		protected void SC_updateShop(ISession session, ShopPOD shop);
#endregion
#region Server-side Interface
		/// <summary>
        /// 商店购买，index=位置，从1开始
        /// </summary>
    	public UniTask<BuyResultData> CS_buy(int shopCid, int index)
        {
            return _CS_buy(shopCid, index);
        }
        
        public UniTask<BuyResultData> _CS_buy(int shopCid, int index)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(BuyResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.shop_buy, "Send->"+"Shop.CS_buy { "
				+ "shopCid="
				+ shopCid
				+ ", index="
				+ index
				+" }");
#endif
        	UniTask<BuyResultData> task = MsgAsyncAwaiter<BuyResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_buy(shopCid, index));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.shop_buyResult);
			return task;
        }
		
		public static SendPackage Pack_for_buy(int shopCid, int index) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.shop_buy;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(shopCid);
			_out.Write_int(index);
			return si_p;
		}
		/// <summary>
        /// 刷新商店,type=1免费刷新2道具刷新3付费刷新
        /// </summary>
    	public UniTask<RefreshResultData> CS_refresh(int shopCid, int type)
        {
            return _CS_refresh(shopCid, type);
        }
        
        public UniTask<RefreshResultData> _CS_refresh(int shopCid, int type)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(RefreshResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.shop_refresh, "Send->"+"Shop.CS_refresh { "
				+ "shopCid="
				+ shopCid
				+ ", type="
				+ type
				+" }");
#endif
        	UniTask<RefreshResultData> task = MsgAsyncAwaiter<RefreshResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_refresh(shopCid, type));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.shop_refreshResult);
			return task;
        }
		
		public static SendPackage Pack_for_refresh(int shopCid, int type) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.shop_refresh;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(shopCid);
			_out.Write_int(type);
			return si_p;
		}
#endregion
#region Messages
        public struct BuyResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public int shopCid;
	        public int index;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                shopCid = _in.HasRemaining() ? _in.Read_int() : default;
                index = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.shop_buyResult;}
            
            public override string ToString()
            {
                return "Shop.buyResult"+ ", code="+ code+ ", shopCid="+ shopCid+ ", index="+ index;
            }
        }
        public struct RefreshResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public int shopCid;
	        public int type;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                shopCid = _in.HasRemaining() ? _in.Read_int() : default;
                type = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.shop_refreshResult;}
            
            public override string ToString()
            {
                return "Shop.refreshResult"+ ", code="+ code+ ", shopCid="+ shopCid+ ", type="+ type;
            }
        }
        public struct UpdateShopData : IReceiveMessage {
	        public ShopPOD shop;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                shop = _in.HasRemaining() ? _in.Read_pod<ShopPOD>(ShopPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.shop_updateShop;}
            
            public override string ToString()
            {
                return "Shop.updateShop"+ ", shop="+ shop;
            }
        }
    }
#endregion
}